package com.hxgz.steel.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.dao.*;
import com.hxgz.steel.domain.Merchant;
import com.hxgz.steel.domain.Page;
import com.hxgz.steel.dto.ExtendMerchantDto;
import com.hxgz.steel.dto.HotStore;
import com.hxgz.steel.dto.MerchantDto;
import com.hxgz.steel.service.MerchantService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 店铺业务层
 */
@Service
@Transactional
public class MerchantServiceImpl implements MerchantService {

  @Autowired
  MerchantMapper mapper;
  @Autowired
  OrderMapper oMapper;
  @Autowired
  AskBuyMapper abMapper;

  /**
   * 新增或编辑店铺信息
   */
  @Override
  public ServerResponse insertOrUpdateMerchant(Merchant merchant) {
    int rowChange = 0;
    if (merchant.getId() == null) {
      merchant.setCreateTime(new Date());
      rowChange = mapper.insertSelective(merchant);
    } else {
      merchant.setUpdateTime(new Date());
      merchant.setStatus(0);
      rowChange = mapper.updateByPrimaryKeySelective(merchant);
    }
    return rowChange == 1 ? ServerResponse.createBySuccessMessage("操作成功 等待审核")
        : ServerResponse.createByErrorMessage("操作失败");
  }

  /**
   * 查询当前登陆用户的商铺信息
   */
  @Override
  public ServerResponse getMerchantByuserId(Long userId) {
    ExtendMerchantDto merchant = mapper.getMerchantByuserId(userId);
    String businessScope="";
    if(merchant!=null){
      businessScope = merchant.getBusinessScope();
    }
    //取出字符串拆分进行对比判断
    if(!StringUtils.isEmpty(businessScope)){
      String[] split = businessScope.split("-");
      if(split.length>0) {
        String str = "";
        for (String s : split) {
          if (s.equalsIgnoreCase("1")) {
            str += "风类 ";
          } else if (s.equalsIgnoreCase("2")) {
            str += "水类 ";
          } else if (s.equalsIgnoreCase("3")) {
            str += "电类 ";
          } else if (s.equalsIgnoreCase("4")) {
            str += "消防类 ";
          }else {
            str+=s;
          }
        }
        merchant.setBusinessScope(str);
      }
    }

    return ServerResponse.createBySuccess(merchant == null ? new Merchant() : merchant);
  }

  public ServerResponse hotMerchant(Page page) {
    PageHelper.startPage(page.getCurrPageNo(), page.getPageSize());
    List<HotStore> merchants = mapper.horMerchant();
    PageInfo pageinfo = new PageInfo(merchants);
    return ServerResponse.createBySuccess(pageinfo);
  }

  /**
   * 根据店铺id获取店铺信息和店铺成单率
   */
  public ServerResponse getMerchantById(Long merchantId) {
    if (merchantId == null) {
      return ServerResponse.createBySuccess();
    } else {
      //查询店铺成单数
      Integer realOrder = oMapper.getRealOrderNum(merchantId);
      //查询店铺的被求购数
      Integer realAsk = abMapper.getRealAsk(merchantId);
      MerchantDto dto = mapper.selectOneByPrimaryKey(merchantId);
      if(dto == null){
        dto = new MerchantDto();
      }
      dto.setRealRate((realAsk == null || realAsk == 0) ? "0" : String.valueOf(realOrder / realAsk));
      return ServerResponse.createBySuccess(dto);
    }
  }

}
